package com.quark.controller.system;

import com.quark.model.system.role.SysRole;
import com.quark.result.RequestResult;
import com.quark.service.system.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 张高昌
 * @version 版本
 * @date 2021/11/11 9:24
 * @description: 功能描述
 */
@Api(tags = "角色管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    @ApiOperation("获取角色列表")
    @GetMapping("roleList")
    public RequestResult roleList() {

        return RequestResult.success(roleService.getPageList());
    }

    @ApiOperation("新增角色")
    @PostMapping("roleAdd")
    @PreAuthorize("@pms.hasPermission('sys_role_add')")
    public RequestResult roleAdd(@RequestBody @Valid SysRole sysRole) {

        return RequestResult.success(roleService.insertRole(sysRole));
    }

    @ApiOperation("修改角色")
    @PostMapping("updateRole")
    @PreAuthorize("@pms.hasPermission('sys_role_update')")
    public RequestResult updateRole(@RequestBody @Valid SysRole sysRole) {

        return RequestResult.success(roleService.updateRole(sysRole));
    }

    @ApiOperation("修改角色状态")
    @GetMapping("updateRoleState")
    public RequestResult updateRoleState(Integer rId, Integer state) {

        return RequestResult.success(roleService.updateRoleState(rId, state));
    }

    @ApiOperation("删除角色")
    @PostMapping("deleteRole/{rId}")
    @PreAuthorize("@pms.hasPermission('sys_role_delete')")
    public RequestResult deleteRole(@PathVariable String rId) {

        return RequestResult.success(roleService.deleteRole(Integer.valueOf(rId)));
    }

    @ApiOperation("获取角色分配的权限")
    @GetMapping("rolePermissions/{rId}")
    public RequestResult rolePermissions(@PathVariable String rId) {

        return RequestResult.success(roleService.getRolePermissions(Integer.parseInt(rId)));
    }
}
